<?php

class Application_Model_Options {

    private $_dbtable;

    public function __construct() {
        $this->_dbtable = new Application_Model_DbTable_Options();
    }

    /**
     * 
     * @param type $name
     * @return type
     */
    public function getByName($name) {
        $select = $this->_dbtable->select();
        $select->where("name = ?", $name);
        $fetchAll = $this->_dbtable->fetchAll($select);
        $formateData = $this->formateData($fetchAll[0]);
        return $formateData;
    }

    /**
     * 
     * @param type $data
     * @return type
     */
    private function formateData($data) {
        $djson = json_decode($data['details']);
        $array = array(
            'id' => $data['id'],
            'name' => $data['name'],
            'details' => $djson
        );
        return $array;
    }

    public function getCloudianryDetails() {
        $byName = $this->getByName("cloudinary");
        $details = $byName['details'];
        $cloudName = ($details == null) ? "" : $details->cloudName;
        $apiKey = ($details == null) ? "" : $details->apiKey;
        $apiSecret = ($details == null) ? "" : $details->apiSecret;
        $array = array(
            'id' => $byName['id'],
            'cloudName' => $cloudName,
            'apiKey' => $apiKey,
            'apiSecret' => $apiSecret,
        );
        return $array;
    }

    /**
     * 
     * @param type $data
     * @param type $id
     * @return type
     */
    public function updateDetails($data, $id) {
        return $this->_dbtable->update($data, "id = $id");
    }

}

